home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
jazlib.arc
/
JZSEARCH.DMO
< prev
next >
Wrap
Text File
|
1988-12-18
|
10KB
|
290 lines
/*
┌────────────────────────────────────────────────────────────────────────────┐
│ jzsearch │
│ Search a given file spec for a given string. │
│ Synopsis: │
│ │
│ Usage JZSEARCH [-uwcr] [filespec] [search string\\replace string] │
│ Options are as follows:\n"); │
│ -u Uppercase (Ignores case on search) │
│ -c Display files on console │
│ -w Whole words only. "MEM" won't be found in "MEMORY" │
│ -r Replace string if found. Syntax for this is:" │
│ -p Prompt for change before replacing │
└────────────────────────────────────────────────────────────────────────────┘
*/
#include <stdio.h>
#include <jzdirect.h>
#define UPPER 1 /* bit flag for upper case */
#define ALL 2 /* bit flag for search all */
#define CONSOLE 4 /* bit flag for console out */
#define REPLACE 8 /* bit flag for replace */
#define WORD 16 /* bit flag for WORD search */
#define PROMPT 32 /* bit flag for prompting change */
#define PUNCT "!@#$%^&*()_+|-=\\~` <>?,./:\";'[]{}\000\009\015\012"
main(argc,argv)
int argc;
char **argv;
{
FILE *fd; /* file variable */
FILE *TMP; /* temporary file variable */
char wstr[256]; /* string to read info into */
char wtemp[256]; /* temp string for loading */
char worig[256]; /* original string for search */
char wsearch[256]; /* string to search on */
char wreplace[256]; /* string to replace wsearch with */
char wpath[65]; /* hold the path string */
char wfspec[65]; /* path + file name */
char wunq[65]; /* unique file name */
char *wch; /* eventual pointer to wstr */
int werr; /* error result of directory search */
TDIR wdir; /* disk transfer buffer for dir search */
int windex; /* position of last "\" in file spec */
int w,wok; /* scratch integer */
int OPTION = 0; /* set option flags */
int woffset; /* offset of command line arguments */
char *wpos,*jzstrpos(); /* position of possible string */
int wlen; /* hold length of a string */
printf("\nJZSEARCH (C) JazSoft by Jack A. Zucker 301-794-5950 | CIS: 75766,1336");
if (argc < 3) {
printf("\nUsage JZSEARCH [-upwcr] [filespec] [search string\\replace string]");
printf("\nOptions are as follows:\n");
printf("\n-u Uppercase (Ignores case on search)");
printf("\n-c Display files on console");
printf("\n-w Whole words only. \"MEM\" won't be found in \"MEMORY\"");
printf("\n-p Prompt for change before replacing");
printf("\n-r Replace string if found. Syntax for this is:");
printf("\n jzsearch -r *.pas begin\\BEGIN");
printf("\n which would replace all \"begin\"s with \"BEGIN\"s");
printf("\n The \"\\\" is the delimiter, use \"\\\\\" to insert a");
printf("\n slash into the search or replace spec.");
printf("\nNote that for straight searches, options are not required.");
printf("\nFor example jzsearch *.pas begin\n");
exit(1);
}
if (getswitch(argv[1],&OPTION))
woffset = 2; /* allow for the options argument */
else
woffset = 1; /* no options argument */
strcpy(wtemp, argv[woffset+1]); /* get search info into wsearch */
/**
** Allow searches on words seperated by spaces by re - creating
** command line. woffset + 2 will yield the second word in the
** search specification if there is one.
**/
for (w = woffset + 2 ; *argv[w] ; w ++) {
strcat(wtemp," "); /* add a space */
strcat(wtemp,argv[w]); /* add new arg */
}
/**
** If we are in replace mode, look for the "\" character as the delimiter
** between the search string and the replace string. If we encounter 2
** "\" chars then we insert one "\" in the string !
**/
*wsearch = 0; /* init search and replace strings */
*wreplace = 0;
if (OPTION & REPLACE) {
strcpy(wunq,"\\"); /* create unique file in root */
jzunqfil(w,wunq,0); /* get unique file name */
jzclsfil(w); /* close it, we only need its name */
for (w = 0 ; w < strlen(wtemp) ; w ++)
if (wtemp[w] == '\\')
if (wtemp[w+1] != '\\') {
parse(wtemp+w+1,wreplace); /* get replace part of string */
wtemp[w] = 0; /* null terminate string */
parse(wtemp,wsearch); /* get search part of string */
break;
}
else
w ++; /* get past double "\" */
if (*wsearch == 0) { /* check for null search string */
printf("\nNothing to search on. Aborting...");
if (OPTION & REPLACE)
unlink(wunq); /* delete unique file here */
exit(1);
}
}
else
strcpy(wsearch,wtemp); /* otherwise only one string */
if (OPTION & UPPER)
strupr(wsearch); /* convert to upper case */
/**
** Check and see whether a path was specified or not
**/
if ((windex = rindex(argv[woffset],'\\')) != -1) {
strncpy(wpath,argv[woffset],windex+1);
*(wpath+windex+1) = 0;
}
else *wpath = 0;
werr = jzfndfst(argv[woffset],32,&wdir); /* find first matching file */
if ( ! werr )
do {
strcpy(wfspec,wpath); /* get path */
strcat(wfspec,wdir.name); /* concatenate file name */
fd = fopen(wfspec,"r"); /* open source file for read */
if (OPTION & REPLACE)
TMP = fopen(wunq,"w"); /* open destin for write */
wlen = strlen(wsearch); /* get length of search string */
while (wch = fgets(wstr,255,fd)) {/* read a line from the file */
wok = 1; /* set flag true */
strcpy(worig,wstr); /* save original copy of string */
if (OPTION & UPPER)
strupr(wstr); /* convert to upper case */
/**
** This if statement checks for the existance of
** the search string in the buffer of chars most
** recently read from the file.
**/
if (wpos = jzstrpos(wsearch,wstr)) {
/**
** If OPTION & WORD, the first character before and after
** the sub - string must either be punctuation characters
** or the first char or last character of the line.
**/
if (OPTION & WORD)
wok = ((wpos == wstr) || (index(PUNCT,*(wpos-1)) != -1)) &&
(index(PUNCT,*(wpos+wlen)) != -1);
/**
** If we are in replace mode and prompt mode
** we will prompt the user before changing the
** string in question.
**/
if (wok && (OPTION & PROMPT) && (OPTION & REPLACE)) {
printf("\nFile name : %s",wfspec);
printf("\nSearch string : %s",wsearch);
printf("\nFull String : %s",worig);
printf("Replace String: %s",wreplace);
printf("\nEnter \"Y\" to confirm change: ");
w = getch();
printf("\n");
wok = (toupper(w) == 'Y');
}
if (wok) {
if (OPTION & REPLACE) { /* are we in replace mode ? */
w = (int) wpos - (int) wstr; /* convert pointer to index */
jzdltstr(worig,w,wlen); /* delete the old string */
jzinsstr(worig,wreplace,w); /* insert the new string */
}
if ( ! (OPTION & CONSOLE) )
printf("\n\"%s\" found in %s\n%s",wsearch,wdir.name,worig);
if (! (OPTION & ALL)) /* don't continue to search this file */
break;
}
}
if (OPTION & CONSOLE) /* print to the console? */
if (OPTION & REPLACE)
fputs(worig,TMP); /* write to temp file to */
else
printf("%s",worig);
}
fclose(fd);
if (OPTION & REPLACE) { /* are we in replace mode? */
fclose(TMP);
unlink(wfspec); /* delete original */
rename(wfspec,wunq); /* rename unq to orig */
}
werr = jzfndnxt(&wdir); /* get next file spec */
} while ( ! werr );
else
printf("\nNo matching files...");
}
/**
** Evaluate switches set by the user and *OR* the appropriate bits
** to *ON* .
** options are as follows:
** UPPER 1
** ALL 2
** CONSOLE 4
** REPLACE 8
** WORD 16
** PROMPT 32
**/
getswitch(fstr,woption)
char *fstr;
int *woption;
{
char ch;
if (*fstr != '-' && *fstr != '/') return(0);
while (ch = *++fstr)
switch(toupper(ch)) {
case 'U' : (*woption) |= UPPER ;
break;
case 'A' : (*woption) |= ALL ;
break;
case 'C' : (*woption) |= (ALL + CONSOLE);
break;
case 'R' : (*woption) |= (ALL + CONSOLE + REPLACE);
break;
case 'W' : (*woption) |= WORD;
break;
case 'P' : (*woption) |= PROMPT;
break;
}
return(1);
}
/**
** Parse the string looking for double "\"
** chars. If we encounter two, then we insert
** one into the string. Later, the routine
** will treat one "\" as the escape char.
**/
parse(fsource,fdestin)
char *fsource,*fdestin;
{
int w,windex;
*fdestin = 0; /* create null string */
windex = 0 ; /* set starting index */
for (w = 0 ; w < strlen(fsource) ; w ++) {
if (fsource[w] == '\\')
if (fsource[w+1] == '\\') {
fdestin[windex] = '\\'; /* put 1 "\" in string */
w ++; /* point past dummy "\" */
}
else ;
else
fdestin[windex] = fsource[w];
windex ++; /* increment index position */
}
fdestin[windex] = 0; /* put terminator in search string */
}